<?php
class SettingParser extends SettingParserCommon
{		

	function parseIn($key, $value) {
		
		//if($key == 'show_file_link' && ) {
		//	$value = 0;
		//}
		
		return $value;
	}
	
	function parseOut($key, $value) {
		return $value;
	}
	
	
	// options parse
	function parseSelectOptions($key, $values) {
		if($key == 'view_format') {
			$options = array('option_1'); //, 'option_4', 'option_2');
			return array_diff_key($values, $options);
		}
		
		return $values;
	}	
	
	
	// when display
	function skipSettingDisplay($key, $value = false) {
		$ret = false;
		if($key == 'show_file_link' && !$this->model->isExtra('file')) {
			$ret = true;
		}// elseif($key == 'private_policy' && !$this->model->isExtra('private')) {
		//	$ret = true;
		//}
		
		return $ret;
	}
	
	
	function parseGroupId($group_id) {
		$sort = array(8=>'2.1');
		return (isset($sort[$group_id])) ? $sort[$group_id] : $group_id;
	}
	
	
	// any special rule to parse form field
	function parseForm($setting_key, $val, $field = false, $setting_msg = false) {
	
		$setting_keys = array('view_template');
		if(in_array($setting_key, $setting_keys)) {
			
			if($val == 'check') {
				return true;
			}
			
			return SettingParseForm::$setting_key($this, $val, $field, $setting_msg);
		}
	
		return false;
	}
}



class SettingParseForm
{

	function view_template($obj, $values, $field, $setting_msg) {
		
		$format_values['1'] = 'default';
		$format_values['2'] = 'tree';
		$format_values['4'] = 'faq';
		
		$options_text = $setting_msg['view_template'];
		unset($options_text['title'], $options_text['descr']);
		//echo "<pre>"; print_r($options_text); echo "</pre>";
	
		$options_values['1.1'] = 'default';
		$options_values['1.2'] = 'noheader';
		//$options_values['1.2'] = 'tree';
	
		$options_values['2.1'] = 'default';
		//$options_values['2.2'] = 'noheader';
		//$options_values['2.3'] = 'noheader';
		
		$options_values['4.1'] = 'default';
		//$options_values['2.2'] = 'noheader';
		//$options_values['2.3'] = 'noheader';

		
		/*
		var myJSONObject = {
			
			"default": [
		        {"key": "PRIVMSG", "val": "newURI", "s": "^http://.*"},
		        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
		        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
		    ],
			
			"defaulta": [
		        {"key": "PRIVMSG", "val": "newURI", "s": "^http://.*"},
		        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
		        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
		    ]
		};
		*/
		
		$selected = $obj->model->getSettings(false, 'view_template');
		$json = array();
		foreach($format_values as $format_key => $format_value) {
			$json_body = array();
			
			foreach($options_values as $k => $v) {
				if($k[0] == $format_key) {
					
					$val  = $options_values[$k];
					$text = $options_text['option_' . $k];
					$s    = ($v == $selected) ? 'true' : 'false';
					
					$json_body[] = sprintf('{"val": "%s", "text": "%s", "s": %s}', $val, $text, $s);
				}
			}
			
			$json[] = sprintf("\"%s\": [\n%s\n]", $format_value, implode(",\n", $json_body));
		}
	
		$json = implode(",\n", $json);

		//echo "<pre>"; print_r($json); echo "</pre>";
		//echo "<pre>"; print_r($options); echo "</pre>";
		//echo "<pre>"; print_r($values); echo "</pre>";
		//echo "<pre>"; print_r($setting_msg); echo "</pre>";
		
		$tpl = &new tplTemplatez(APP_MODULE_DIR . 'setting/template/form_view_template.ini');
		
		$tpl->tplAssign('myOptionsJson', $json );
		$tpl->tplAssign('field', $field);
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
}
?>